package com.wise.aip.service.impl;

import java.util.Map;

import com.wise.aip.service.BaseDataService;
import com.wise.aip.template.ParamTemplate;
import com.wise.aip.template.URLTemplate;
import com.wise.aip.token.TokenKit;

/**
 * 
 * Title: BankDaoImpl <br>
 * Description: 金融机构业务层
 * 
 * @author yacong_liu Email:2682505646@qq.com
 * @date 2019年4月13日
 */
public class BankServiceImpl extends BaseDataService {

    /**
     * 
     * <p>
     * Title: save
     * </p>
     * <p>
     * Description: 新增金融机构
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param authorityCode 授权码
     * @param itemClassCode 基础资料类型码 eg:Bank
     * @param params 参数 key: name number
     * @return
     * @throws Exception
     */
    public String saveBank(String gatewayHost, String authorityCode, String itemClassCode,
            Map<String, String> params) throws Exception {

        StringBuffer log = new StringBuffer();
        String name = "";
        String number = "";
        if (params.size() > 0) {
            name = params.get("name");
            number = params.get("number");
        }

        log.append("**************" + this.getClass().getName() + "_saveBank （新增金融机构）START******\n");
        log.append("**************入参名称：" + name + " 编码：" + number);

        String requestURL = URLTemplate.save(gatewayHost, itemClassCode,
                TokenKit.getAccessToken(gatewayHost, authorityCode));
        String param = ParamTemplate.save(name, number);
        String saveRes = super.save(requestURL, param);

        log.append("\n*************请求URL:" + requestURL);
        log.append("\n*************请求参数:" + param);
        log.append("\n*************响应结果：" + saveRes);
        log.append("\n**************************************新增金融机构END******************");

        System.out.println(log.toString());
        return saveRes;

    }

    /**
     * 
     * <p>
     * Title: updateBank
     * </p>
     * <p>
     * Description: 修改金融机构
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param authorityCode 授权码
     * @param itemClassCode 基础资料类型码 eg:Bank
     * @param params 参数 key: name number oldNumber
     * @return
     * @throws Exception
     */
    public String updateBank(String gatewayHost, String authorityCode, String itemClassCode,
            Map<String, String> params) throws Exception {

        StringBuffer log = new StringBuffer();
        String name = "";
        String number = "";
        String oldNumber = "";
        if (params.size() > 0) {
            name = params.get("name");
            number = params.get("number");
            oldNumber = params.get("oldNumber");
        }

        log.append("**************" + this.getClass().getName() + "_updateBank （修改金融机构）START******\n");
        log.append("**************入参名称：" + name + " 编码：" + number);

        String requestURL = URLTemplate.update(gatewayHost, itemClassCode,
                TokenKit.getAccessToken(gatewayHost, authorityCode));
        String param = ParamTemplate.update(oldNumber, name, number);
        String updateRes = super.update(requestURL, param);

        log.append("\n*************请求URL:" + requestURL);
        log.append("\n*************请求参数:" + param);
        log.append("\n*************响应结果：" + updateRes);
        log.append("\n**************************************修改金融机构END******************");

        System.out.println(log.toString());
        return updateRes;

    }

    /**
     * 
     * <p>
     * Title: deleteBank
     * </p>
     * <p>
     * Description: 删除金融机构
     * </p>
     * 
     * @param gatewayHost 网关地址 eg:http://218.69.112.158:8000/K3API
     * @param authorityCode 授权码
     * @param itemClassCode 基础资料类型码 eg:Bank
     * @param number 需删除的编码
     * @return
     * @throws Exception
     */
    public String deleteBank(String gatewayHost, String authorityCode, String itemClassCode, String number)
            throws Exception {

        StringBuffer log = new StringBuffer();
        log.append("**************" + this.getClass().getName() + "_deleteBank （删除金融机构）START******\n");
        log.append("**************入参编码：" + number);

        String requestURL = URLTemplate.delete(gatewayHost, itemClassCode,
                TokenKit.getAccessToken(gatewayHost, authorityCode));
        String deleteRes = super.deleteOrforbid(requestURL, number);

        log.append("\n*************请求URL:" + requestURL);
        log.append("\n*************请求参数:" + number);
        log.append("\n*************响应结果：" + deleteRes);
        log.append("\n**************************************删除金融机构END******************");

        System.out.println(log.toString());
        return deleteRes;
    }

}
